package com.project.feiController;

import com.project.DTO.AdminAndInsuranceDTO;
import com.project.bo.TravelPdfBO;
import com.project.feign.OrderClient;
import com.project.pojo.InsuranceTravel;
import com.project.pojo.Order;
import com.project.pojo.Ordercopy;
import com.project.pojo.Responsibility;
import com.project.service.*;
import com.project.utils.GeneratePdf;
import com.project.utils.ResponseData;
import com.project.utils.ResultEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OrderFeiController implements OrderClient{
    private final int PASS=1;
    private final int NOT_PASS=0;
    private final int CHECKING=3;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderCopyService orderCopyService;
    @Autowired
    InsuranceTravelService travelService;
    @Autowired
    RealInsuranceTravelService realInsuranceTravelService;
    @Autowired
    ResponsibilityService responsibilityService;
    @Override
    public ResponseData updateOrderWithSuccessByOne(AdminAndInsuranceDTO adminAndInsuranceDTO) {
        Order order = orderService.getOrderById(adminAndInsuranceDTO.getInsuranceId());
        Ordercopy orderCopy = orderCopyService.getOrderCopyById(adminAndInsuranceDTO.getInsuranceId());
        if (order!=null&&order.getDistributedstate()==orderCopy.getDistributedstate()){
                order.setPassOne(PASS);
                order.setOneSuggest(adminAndInsuranceDTO.getMessage());
                order.setCheck(CHECKING);
                orderService.updateOrder(order);
                orderCopy.setPassOne(PASS);
                orderCopy.setOneSuggest(adminAndInsuranceDTO.getMessage());
                orderCopy.setCheck(CHECKING);
                orderCopyService.updateById(orderCopy);
                return new ResponseData(ResultEnums.UPDATED);
        }
        return new ResponseData(ResultEnums.NOT_FOUND);
    }

    @Override
    public ResponseData updateOrderWithSuccessByTwo(AdminAndInsuranceDTO adminAndInsuranceDTO) {
        Order order = orderService.getOrderById(adminAndInsuranceDTO.getInsuranceId());
        Ordercopy orderCopy = orderCopyService.getOrderCopyById(adminAndInsuranceDTO.getInsuranceId());
        if (order!=null&&order.getDistributedstate()==orderCopy.getDistributedstate()){
            order.setPassTwo(PASS);
            order.setTwoSuggest(adminAndInsuranceDTO.getMessage());
            order.setCheck(PASS);
            orderService.updateOrder(order);
            orderCopy.setPassTwo(PASS);
            orderCopy.setTwoSuggest(adminAndInsuranceDTO.getMessage());
            orderCopy.setCheck(PASS);
            orderCopyService.updateById(orderCopy);
            TravelPdfBO travelBo = travelService.getTravelBoById(order.getId());
            travelBo.setResponsibilityList(responsibilityService.getResByType(order.getInsurType()));
            String savepath = GeneratePdf.GenerateTravelPdf(travelBo);
            InsuranceTravel insuranceTravel = new InsuranceTravel(travelBo.getPolicyholderName(), travelBo.getPolicyholderPhonenumber(), travelBo.getPolicyholderDocumenttype(),
                    travelBo.getPolicyholderIdcardnumber(), travelBo.getInsuredName(), travelBo.getInsuredPhonenumber(),
                    travelBo.getInsuredDocumenttype(), travelBo.getInsuredIdcardnumber(), travelBo.getRelationship(),
                    travelBo.getInsuranceamount(), travelBo.getInsurancestarttime(), travelBo.getInsuranceendtime(),
                    travelBo.getInsurancestatus(), travelBo.getInsuredage(), travelBo.getInsuredprofession(),
                    travelBo.getIntendedcity(), savepath);
            realInsuranceTravelService.insert(insuranceTravel);
            return new ResponseData(ResultEnums.UPDATED);
        }
        return new ResponseData(ResultEnums.NOT_FOUND);
    }

    @Override
    public ResponseData updateOrderWithFailure( AdminAndInsuranceDTO adminAndInsuranceDTO) {
        Order order = orderService.getOrderById(adminAndInsuranceDTO.getInsuranceId());
        if (order!=null){
            if (order.getPassOne()!=0){
                order.setPassTwo(NOT_PASS);
                order.setTwoSuggest(adminAndInsuranceDTO.getMessage());
                order.setCheck(NOT_PASS);
                orderService.updateOrder(order);
                return new ResponseData(ResultEnums.UPDATED);
            }else {
                order.setPassOne(NOT_PASS);
                order.setCheck(NOT_PASS);
                order.setOneSuggest(adminAndInsuranceDTO.getMessage());
                orderService.updateOrder(order);
                return new ResponseData(ResultEnums.UPDATED);
            }
        }
        return new ResponseData(ResultEnums.NOT_FOUND);
    }
}
